<script setup>
/**
 * 顶部导航栏组件：
 * - 展示主导航、搜索、购物车、用户头像与登出
 * - 从 localStorage 读取登录状态与当前用户
 * - 监听 storage、cartUpdated 事件以保持状态同步
 * - 点击头像跳转个人主页 /profile
 */
import { ref, onMounted } from 'vue'
import { RouterLink, useRouter } from 'vue-router'
import { isUserLoggedIn, getCurrentUser, getCartItemCount } from '../services/dataService'

// 定义props
const props = defineProps({
  showCartBadge: {
    type: Boolean,
    default: true
  }
})

// 响应式数据
const isLoggedIn = ref(false)
const currentUser = ref(null)
const cartItemCount = ref(0)

// 检查用户登录状态
const checkLoginStatus = () => {
  isLoggedIn.value = isUserLoggedIn()
  currentUser.value = getCurrentUser()
}

// 更新购物车数量
const updateCartItemCount = () => {
  cartItemCount.value = getCartItemCount()
}

// 登出功能
const handleLogout = () => {
  localStorage.removeItem('user')
  localStorage.removeItem('isLoggedIn')
  isLoggedIn.value = false
  currentUser.value = null
  window.dispatchEvent(new Event('storage'))
}

onMounted(() => {
  checkLoginStatus()
  updateCartItemCount() // 初始化购物车数量
  
  // 监听localStorage变化
  window.addEventListener('storage', checkLoginStatus)
  
  // 监听购物车更新事件
  window.addEventListener('cartUpdated', updateCartItemCount)
})

const router = useRouter()
const goProfile = () => {
  router.push('/profile')
}
</script>

<template>
  <header class="app-header">
    <div class="header-content">
      <div class="logo-container">
        <RouterLink to="/" class="logo-link">
          <span class="logo-text">花时</span>
          <span class="logo-bell">🔔</span>
        </RouterLink>
      </div>
      
      <nav class="main-nav">
        <RouterLink to="/" class="nav-link" active-class="active">主导航</RouterLink>
        <RouterLink to="/flowers" class="nav-link" active-class="active">花材分类</RouterLink>
        <RouterLink to="/" class="nav-link" active-class="active">场景推荐</RouterLink>
        <RouterLink to="/" class="nav-link" active-class="active">花艺课程</RouterLink>
        <RouterLink to="/" class="nav-link" active-class="active">会员中心</RouterLink>
      </nav>
      
      <div class="header-actions">
        <!-- 搜索图标 -->
        <div class="search-icon-container">
          <svg class="search-icon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="11" cy="11" r="8"></circle>
            <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
          </svg>
        </div>
        
        <!-- 购物车 -->
        <div class="cart-container">
          <svg class="cart-icon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="9" cy="21" r="1"></circle>
            <circle cx="20" cy="21" r="1"></circle>
            <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
          </svg>
          <span v-if="props.showCartBadge && cartItemCount > 0" class="cart-badge">{{ cartItemCount }}</span>
        </div>
        
        <!-- 用户控制 -->
        <template v-if="isLoggedIn">
          <div class="user-profile">
            <div class="user-avatar" @click="goProfile" title="个人主页">{{ currentUser?.username?.charAt(0).toUpperCase() || 'U' }}</div>
            <span class="user-name">{{ currentUser?.username || '用户' }}</span>
            <button class="logout-button" @click="handleLogout" title="退出登录">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path>
                <polyline points="16 17 21 12 16 7"></polyline>
                <line x1="21" y1="12" x2="9" y2="12"></line>
              </svg>
            </button>
          </div>
        </template>
        <RouterLink v-else to="/login" class="login-button">登录/注册</RouterLink>
      </div>
    </div>
  </header>
</template>

<style scoped>
.app-header {
  background: white;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo-link {
  display: flex;
  align-items: center;
  gap: 5px;
  text-decoration: none;
  color: #333;
}

.logo-text {
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.logo-bell {
  font-size: 18px;
  color: #333;
}

.main-nav {
  display: flex;
  gap: 30px;
}

.nav-link {
  color: #666;
  font-size: 16px;
  font-weight: 500;
  text-decoration: none;
  padding: 10px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-link:hover {
  color: #8E8C41;
}

.nav-link.active {
  color: #8E8C41;
}

.nav-link.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: #8E8C41;
  border-radius: 3px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.search-icon-container {
  cursor: pointer;
  padding: 8px;
  color: #666;
}

.search-icon {
  color: #666;
}

.cart-container {
  position: relative;
  cursor: pointer;
  padding: 8px;
  color: #666;
}

.cart-icon {
  color: #666;
  font-size: 18px;
  transition: color 0.3s;
}

.cart-container:hover .cart-icon {
  color: #8E8C41;
}

.cart-badge {
  position: absolute;
  top: 0;
  right: 0;
  background: #ff6b6b;
  color: white;
  font-size: 0.7rem;
  font-weight: bold;
  padding: 0.15rem 0.4rem;
  border-radius: 50%;
  min-width: 1.2rem;
  text-align: center;
  transform: translate(50%, -50%);
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 15px;
  background: #f8fafc;
  border-radius: 25px;
  transition: all 0.3s ease;
}

.user-profile:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #8E8C41;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.logout-button {
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logout-button:hover {
  background: #e2e8f0;
  color: #666;
}

.login-button {
  padding: 8px 16px;
  background: #8E8C41;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-button:hover {
  background-color: #7A7835;
}

@media (max-width: 768px) {
  .header-content {
    padding: 0 15px;
  }
  
  .main-nav {
    display: none;
  }
  
  .user-name {
    display: none;
  }
}
</style>